package com.gltc.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.sql.rowset.serial.SerialBlob;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.gltc.entities.PhotoAuthUpload;
import com.gltc.entities.UploadOraclePhoto;
import com.gltc.service.PhotoAuthUploadService;
import com.gltc.service.UploadOracleService;
import com.gltc.util.DetectFaceDemo;
import com.gltc.util.StringUtil;

/**
 * 上传图片审核功能
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/")
public class PhotoAuthUploadController {

	
	@Autowired
	private PhotoAuthUploadService authUploadService;
	
	@Autowired
	private UploadOracleService uploadOracleService;
	
	/**
	 * 上传图片
	 * @param file
	 * @param request
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/upload")
    @ResponseBody
    public String handleFileUpload(@RequestParam("file")MultipartFile file,@RequestParam("zfzmhm")String zfzmhm, HttpServletRequest request) throws Exception{
		if(!file.isEmpty()){
			
			//图片本地服务器磁盘路径
			String filePath = System.getProperty("user.dir")+"/src/main/webapp/photo/"+file.getOriginalFilename();
			
			//图片服务器访问路径
			String photoPath = request.getContextPath()+"/photo/"+file.getOriginalFilename();
			try {
				BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(filePath)));
				out.write(file.getBytes());
				out.flush();
				out.close();
			}catch(FileNotFoundException e) {
				e.printStackTrace();
				return "上传失败,"+e.getMessage();
			}catch (IOException e) {
				e.printStackTrace();
				return "上传失败,"+e.getMessage();
			}
			
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			
			//测试上传Oracle数据库
			UploadOraclePhoto uo = new UploadOraclePhoto();
			uo.setId(StringUtil.createPK());
			uo.setPhoto(file.getBytes());
			uo.setSfzmhm(zfzmhm);
			uo.setSctime(sdf.format(new Date()));
			uploadOracleService.insertPhoto(uo);
			
			
			//检测上传图片是否合格
			System.loadLibrary("/src/main/resources/opencv_java249");  
		    String res = new DetectFaceDemo().run(request,filePath); 
		    
		    if("1".equals(res)){
		    	
		    	//TODO  存储日志信息
		    	PhotoAuthUpload p = new PhotoAuthUpload();
		    	p.setPhotopath(photoPath);
		    	p.setZfzmhm(zfzmhm);
		    	p.setUploadPhotoTime(sdf.format(new Date()));
		    	
		    	authUploadService.addPhotoLog(p); //保存日志进数据库
		    	
		    	//检测合格上传到oracle数据库
		    	
		    	
		    	
		    }
			return "1".equals(res)?"检测合格，请上传下一个...":res;
		}else{
			return "上传失败，因为文件是空的.";
		}
    }
}
